var mongoose=require('mongoose');
var User=mongoose.model('User',{
    name:String,
    number:Number,
    comments:Array

});
var db=mongoose.connect('mongodb://localhost/MongoDB_QuanWei_2');


User.update({name:'A'},{number:1101,comments:[
    {title:'1-title-1',content:'1-context-1'},{title:'1-title-2',content:'1-context-2'},
    {title:'1-title-3',content:'1-context-3'},{title:'1-title-4',content:'1-context-4'},
    {title:'1-title-5',content:'1-context-5'},{title:'1-title-6',content:'1-context-6'},
    {title:'1-title-7',content:'1-context-7'},{title:'1-title-8',content:'1-context-8'}
]},{upsert:true},(e,r)=>{
    if(e)throw e;
    console.log('Update:\n',r);

    User.update({name:'B'},{number:1201,comments:[
        {title:'2-title-1',content:'2-context-1'},{title:'2-title-2',content:'2-context-2'},
        {title:'2-title-3',content:'2-context-3'},{title:'2-title-4',content:'2-context-4'},
        {title:'2-title-5',content:'2-context-5'},{title:'2-title-6',content:'2-context-6'},
        {title:'2-title-7',content:'2-context-7'},{title:'2-title-8',content:'2-context-8'}
    ]},{upsert:true},(e,r)=>{
        if(e)throw e;
        console.log('Update:\n',r);


        User.find((e,r)=>{
            if(e)throw e;
            console.log('\nFind All:\n',r);

            User.find({'comments.title':/1\-title/},{
                'comments.$':1
            },(e,r)=>{
                if(e)throw e;
                console.log('\nFind 1:\n',JSON.stringify(r,null,2));

                User.find({'comments.title':/1\-title/},{
                    'comments.$':200
                },(e,r)=>{
                    if(e)throw e;
                    console.log('\nFind 2:\n',JSON.stringify(r,null,2));
                    process.exit(1);
                });

            });

        });

    });

});


/*？？*/